﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace PokerStar.Core.Rules
{
    public class StraightFlushRule : BasicRule
    {
        private IRule _straight = new StraightRule(null);

        public StraightFlushRule(IRule next)
            : base(next)
        {
        }

        protected override GameResult Match(RuleContext context)
        {
            if (context.GroupBySuit.First().Count() >= 5)
            {
                var flush = context.GroupBySuit.First();

                var ss = _straight.Test(new RuleContext(flush.ToList()));
                if (ss != null)
                {
                    var rs = new StraightFlushGameResult(flush.Key);
                    rs.Cards = ss.Cards;
                    rs.Evidence = ss.Evidence;
                    return rs;
                }
            }
            return null;
        }
    }

    public class StraightFlushGameResult : GameResult
    {
        public Suit Flush { get; private set; }

        public StraightFlushGameResult(Suit flush)
        {
            Flush = flush;
        }

        public override ResultLevel Level
        {
            get { return ResultLevel.StraightFlush; }
        }

        public override string ToString()
        {
            var name = Evidence[0] == 14 ? "Royal" : "Straight";
            return string.Format("{6} flush ({5}) -> {0} {1} {2} {3} {4}", Cards[0], Cards[1], Cards[2], Cards[3], Cards[4], (char)Flush, name);
        }
    }
}
